package algorithms;

import wheel.M209Wheels;
import bar.Bar;
import bar.M209Bars;

public class GetBeaufortKey {

    public static int getResultingInt(M209Bars bars, M209Wheels wheels) {
	int sum = 0;
	boolean effective = false;

	for (Bar bar : bars.getBars()) {

	    // Source: http://en.wikipedia.org/wiki/M-209
	    // Key wheel pins come into play when they reach the lower part of the key wheel during
	    // rotation; it is here that they may contact or release the guide arm that deflects the
	    // lugs to the left. The active pin is offset by a particular amount from the letter
	    // currently being displayed on the front of the key wheel; when "AAAAAA" is showing on
	    // the key wheels, the pins that are in play are those associated with the letters
	    // "PONMLK", from left to right

	    for (int id = 1, index = 0; id <= 6; id++, index++) {
		if ((bar.getLug1().getVal() == id || bar.getLug2().getVal() == id) && wheels.getKeyWheels().get(index).getKeyPins().get(wheels.getKeyWheels().get(index).getActiveKey())) {
		    effective = true;
		}
	    }

	    if (effective) {
		sum++;
	    }
	    effective = false;
	}

	return sum;
    }

}
